package com.dw.etl.core.schedu.job;

import com.dw.etl.common.util.SpringBeanUtil;
import com.dw.etl.core.api.CommonService;
import com.dw.etl.core.api.EtlBaseService;
import com.dw.etl.core.api.EtlRunService;
import com.dw.etl.core.bean.model.EtlBase;
import com.dw.etl.core.utils.EtlUtil;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by liujunlei on 2021/6/7.
 */
@Component
public class JobHandler {

    @Value("${now.date}")
    private String nowDate;
    @Autowired
    CommonService commonService;
    @Autowired
    EtlRunService etlRunService;

    /**
     * 就以30为界，30天之内的只存储，30+1的实现统计。30是个定义变量如何。
     * 现场沟通病案的更新问题，如果更新没有明确标志，数据的准确性会存疑
     */
    @XxlJob("dayBadataXxljob")
    public void dayBadataXxljob() throws Exception {
        String param = XxlJobHelper.getJobParam();
        System.out.println("dayBadataXxljob被调用"+param);
        // default success
    }

    /**
     *  按天抽取基础数据
     *  hos->ods->dw->dm
     */
    @XxlJob("dayBasedataXxljob")
    public void dayBasedataXxljob() throws Exception {
        Date triggerTime = new Date();
        JobParametersBuilder parametersBuilder = new JobParametersBuilder();
        commonService.addOrgJobParameter(parametersBuilder);
        EtlUtil.addDayParameter(nowDate,parametersBuilder);
        etlRunService.runEtlBaseJobByDay(nowDate,"hosToOds","dayBasedataXxljob",triggerTime);
        etlRunService.runEtlBaseJobByDay(nowDate,"odsToDw","dayBasedataXxljob",triggerTime);
        etlRunService.runDmJObByDay(nowDate,"dayBasedataXxljob",triggerTime);
    }

    /**
     *  按天检测更新
     *  更新场景：
     *   1. 取消出院
            select *from 住院记录 where 取消标志=true and 取消出院时间='';
            跟事实表dbc_inp相关的基础指标，->复合指标执行数据更新
         2. 非当天退号
     */
    @XxlJob("dayChkUpdateXxljob")
    public void dayChkUpdateXxljob() throws Exception {
        String param = XxlJobHelper.getJobParam();
        System.out.println("dayChkUpdateXxljob被调用"+param);
        // default success
    }

    /**
     *  按分钟抽取实时数据
     */
    @XxlJob("minHosdataXxljob")
    public void minHosdataXxljob() throws Exception {
        Date triggerTime = new Date();
        JobParametersBuilder parametersBuilder = new JobParametersBuilder();
        commonService.addOrgJobParameter(parametersBuilder);
        EtlUtil.addDayParameter(nowDate,parametersBuilder);
        etlRunService.runEtlBaseJobByDay(nowDate,"minToDm","minHosdataXxljob",triggerTime);
    }

    /**
     *  按小时抽取实时数据
     */
    @XxlJob("hourHosdataXxljob")
    public void hourHosdataXxljob() throws Exception {
        String param = XxlJobHelper.getJobParam();
        System.out.println("hourHosdataXxljob被调用"+param);
        // default success
    }
}
